/*
 * FWK.Http
 * 主要是针对 浏览器地址栏中href 操作
 */
FWK.Http = {
	/**
	 * 扩展本身
	 * @param {Object} sName
	 */
	extra:FWK.extra,
	/**
	 * 获取 sName对应的 document.location.search 值
	 * @param {String} sName
	 */
	getQuery:function(sName){
		var sRE = "([&?])+" + sName + "=([^&]*)&?";
        var oRE = new RegExp(sRE);
        if(oRE.test(document.location.search)) {
            return decodeURIComponent(RegExp["$2"]);
        } else {
            return null;
        }
	},
	/**
	 * 返回 数组
	 */
	getQueryArr:function(){
		var rArr = [];
		for(var i=0;i<arguments.length;i+=1){
			rArr[i] = this.getQuery(arguments[i]);
		}
	},
	/**
	 * 返回 对象
	 */
	getQueryObj:function(){
		var rArr = {};
		for(var i=0;i<arguments.length;i+=1){
			rArr[arguments[i]] = this.getQuery(arguments[i]);
		}
	},
	/**
	 * {；暂且不是很明白 hash如何构成 暂时定为 以;分割的一串数据
	 * 获取 sName对应的 document.location.hash 值
	 * @param {String} sName
	 */
	getHash:function(sName){
		var sRE = "(;)?" + sName + "=([^;]*)&?";
        var oRE = new RegExp(sRE);
        if(oRE.test(document.location.hash.slice(1))) {
            return decodeURIComponent(RegExp["$1"]);
        } else {
            return null;
        }
	},
	/**
	 * 返回 数组
	 */
	getHashArr:function(){
		var rArr = [];
		for(var i=0;i<arguments.length;i+=1){
			rArr[i] = this.getHash(arguments[i]);
		}
	},
	/**
	 * 返回 对象
	 */
	getHashObj:function(){
		var rArr = {};
		for(var i=0;i<arguments.length;i+=1){
			rArr[arguments[i]] = this.getHash(arguments[i]);
		}
	},
	/**
	 * 获取域名
	 */
	getDomain:FWK.getDomain,
	/**
	 * 创建 http 请求对象
	 */
	create:function(){
		var http;
		try{
			http = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try{
				http = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
				try{
					http = new XMLHttpRequest();
				}
				catch(e) {
					http = null;
				}
			}
		}
		return http;
	},
	/**
	 * 获取 sName对应的Cookie值
	 * @param {String} sName
	 */
	getCookie:function(sName){
		var sRE = "(?:; )?" + sName + "=([^;]*);?";
        var oRE = new RegExp(sRE);
        if (oRE.test(document.cookie)) {
            return decodeURIComponent(RegExp["$1"]);
        } else {
            return null;
        }
	},
	/**
	 * 返回 数组
	 */
	getCookieArr:function(){
		var rArr = [];
		for(var i=0;i<arguments.length;i+=1){
			rArr[i] = this.get(arguments[i]);
		}
	},
	/**
	 * 返回 对象
	 */
	getCookieObj:function(){
		var rArr = {};
		for(var i=0;i<arguments.length;i+=1){
			rArr[arguments[i]] = this.get(arguments[i]);
		}
	},
	/**
	 * 设置 sName 对应的Cookie值  返回EJ.Cookie FWK.Cookie.set().set()....
	 * @param {Object} sName	key
	 * @param {String} sValue	value
	 * @param {Date} oExpires	有效期
	 * @param {String} sPath	文件
	 * @param {String} sDomain	域名
	 * @param {String} bSecure	未知
	 */
	setCookie:function(sName, sValue, oExpires, sPath, sDomain, bSecure){
		var sCookie = sName + "=" + encodeURIComponent(sValue);
		if (oExpires) {
			sCookie += "; expires=" + oExpires.toGMTString();
		}
		if (sPath) {
			sCookie += "; path=" + sPath;
		}
		if (sDomain) {
			sCookie += "; domain=" + sDomain;
		}
		if (bSecure) {
			sCookie += "; secure";
		}
		document.cookie = sCookie;
		return this;
	},
	/**
	 * 删除Cookie中对应sName中的值 返回EJ.Cookie FWK.Cookie.remove().remove()....
	 * @param {String} sName
	 * @param {String} sPath
	 * @param {String} sDomain
	 */
	removeCookie:function(sName, sPath, sDomain){
		var sCookie = sName + "=; expires=" + (new Date(0)).toGMTString();
        if (sPath) {
            sCookie += "; path=" + sPath;
        }
        if (sDomain) {
            sCookie += "; domain=" + sDomain;
        }
        document.cookie = sCookie;
		return this;
	}
};

/* Ajax请求 */
FWK.Http.Ajax = FWK.Class.extend({
	/**
	 * 异步请求时候 相应 onreadystatechange
	 */
	_askChange:function(){
		if(this.HO.readyState==4){
			this._askReady(true);
		}
	},
	/**
	 * Ajax 回调
	 * @param {boolen} flag true 异步 false 同步
	 */
	_askReady:function(flag){
		if(this.blurer){
			clearTimeout(this.blurer);
		}
		if(this.HO.status == 200) {
			var rV;
			switch (this.contentType) {
				case "text/html":
					rV =  this.HO.responseText;
					break;
				case "text/xml":
					rV =  FWK.loadXML(this.HO.responseText);
					break;
				case "text/json":
				case "text/javascript":
				case "application/javascript":
				case "application/x-javascript":
					try {rV =  eval("(" + this.HO.responseText + ")");}catch (e) {rV =  this.HO.responseText;}
					break;
				default:
					rV = FWK.loadXML(this.HO.responseText);
					if(!rV) {
						try {rV = eval("(" + this.HO.responseText + ")");} catch (e) {rV = this.HO.responseText;};
					}
			}

			setTimeout(this._destroy.bind(this),10);

			if(flag){
				try{this.onSuccessed(rV);}catch(e){}
			}
			else{
				return rV;
			}
		}
		else{
			setTimeout(this._destroy.bind(this),10);
			if(flag){
				try{this.onFailed();}catch(e){};
			}
			else{
				return null;
			}
		}
	},
	/**
	 * 对 参数的格式化
	 * @param {Object||String} parms
	 */
	_askQuery:function(parms){
		var strs = [];
		if (parms) {
			if (FWK.isStr(parms)) {
				strs.push(_ParameterObj);
			}
			else {
				for (var n in parms) {
					strs.push(n + "=" + encodeURIComponent(parms[n]));
				}
			}
		}
		return strs.join("&");
	},
	/**
	 * 取消Ajax请求
	 */
	_blur:function(){
		this.HO.abort();
		try{this.onAbort();}catch(e){}
	},
	/**
	 * 销毁Ajax请求
	 */
	_destroy:function(){
		for(var n in this){
			this[n] = null;
		}
	},
	/**
	 * 设置Ajax头
	 * @param {Object} requestType
	 */
	setRequestHeader:function(requestType){
		if (FWK.isStr(requestType)) {
			this.HO.setRequestHeader("Content-Type", requestType);
			this.contentType = requestType;
		}
		else if(FWK.isObj(requestType)){
			for (var n in requestType) {
				XHO.setRequestHeader(n, requestType[n]);
				if (n == "Content-Type") {
					this.contentType = requestType[n];
				}
			}
		}

		if (this.HO.overrideMimeType) {
			this.HO.overrideMimeType('text/xml');
		}

		return this;
	},
	/**
	 * 构建函数
	 * @param {Object} transType
	 * @param {Object} requestType
	 */
	$init:function(transType,requestType){
		this.HO = FWK.Http.create();
		this.transType = (transType.toUpperCase() == "POST")?"POST":"GET";
		this.requestType = requestType || this.requestType;
	},
	/**
	 * Ajax请求
	 * @param {Boolen} async
	 * @param {String} url
	 * @param {Object || String} parms
	 * @param {Number} ovTime 单位秒
	 */
	ask:function(async,url,parms,ovTime){
		var aUrl = url + ((url.indexOf("?") > -1) ? "&r=" : "?r=") + Math.random(1000);
		var parmStr = this._askQuery(parms);
		if(this.transType == "POST") {
			this.HO.open("POST", aUrl, async);
			this.HO.setRequestHeader("Method", "POST "+aUrl+" HTTP/1.1");
			this.HO.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			parmStr = parmStr.replace(/[\x00-\x08\x11-\x12\x14-\x20]/g, "*");
		}
		else {
			this.HO.open("GET", aUrl + "&" + parmStr, async);
			parmStr = null;
		}

		if(async){
			this.HO.onreadystatechange = this._askChange.bind(this,true);
			this.HO.send(parmStr);
		}
		else{
			this.HO.send(parmStr);
			return this._askReady(false);
		}

		if(FWK.isNum(ovTime) && ovTime>0){
			this.blurer = setTimeout(this._blur.bind(this),ovTime*1000);
		}
	},
	/**
	 * 同步请求
	 */
	syncAsk:function(url,parms,ovTime){
		return this.ask(false,url,parms,ovTime);
	},
	/**
	 * 异步请求
	 */
	asyncAsk:function(url,parms,ovTime){
		this.ask(true,url,parms,ovTime);
	}
});